<?php
/**
 * 应用公共基类
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/16
 * Time: 15:53
 */

namespace common\commands;


use common\models\AutoValidate;

class BaseController extends \yii\web\Controller{
    public $head, $title, $js, $css;
    private function head(){
        $css = $this->css;
        $js = $this->js;
        $html_css = '';
        $html_js = '';
        if(isset($css['path']) && is_array($css['path'])){
            //$css = implode($css, '');
            $prefix = '';
            if(isset($css['prefix']))
                $prefix = $css['prefix'];

            $version = '';
            if(!empty(\Yii::$app->params['head_v']))
                $version = '?v='.\Yii::$app->params['head_v'];
            $path = $css['path'];
            foreach($path as $k=>$v){
                $_css = $v;

                $path[$k] = '<link type="text/css" href="'.$prefix.$_css.$version.'" rel="stylesheet" />';
            }
            $html_css = implode($path, '');;
        }
        if(isset($js['path']) && is_array($js)){
            //$css = implode($css, '');
            $prefix = '';
            if(isset($js['prefix']))
                $prefix = $js['prefix'];

            $version = '';
            if(isset($js['v']))
                $version = '?v='.$js['v'];
            $path = $js['path'];
            foreach($path as $k=>$v){
                $_js = $v;

                $path[$k] = '<script language="javascript" src="'.$prefix.$_js.$version.'"></script>';
            }
            $html_js = implode($path, '');;
        }
        return $html_css.$html_js;
    }

    public function _render($view, $data = '', $drive = false){
        $result = array();
        if($data)
            $result = $data;
        if(isset($data['input']) && is_array($data['input'])){
            $input = array();
            foreach($data['input'] as $k=>$v){
                if(isset($v['t']) && isset($k)){
                    $p = '';
                    $c = '';
                    if(isset($v['p']))
                        $p = $v['p'];
                    if(isset($v['c']))
                        $c = $v['c'];
                    $id = $k;
                    if(isset($v['id']))
                        $id = $v['id'];
                    $data_validate = AutoValidate::get($k);
                    $r = $data_validate['rule'];
                    $m = $data_validate['msg'];
                    $input[$k] = '<input type="'.$v['t'].'" id="'.$id.'" class="'.$c.'" name="'.$k.'" placeholder="'.$p.'" rule="'.$r.'" msg="'.$m.'" value="" required/>';
                }
            }
            unset($data['input']);
            $result['input'] = $input;
        }
        $head = $this->head();
        if($head)
            $this->head = $head;
        if($result)
            return $this->render($view, $result);
        else
            return $this->render($view);
    }

    protected function request(){
        return \Yii::$app->request;
    }
    protected function post(){
        return $this->request()->post();
    }
    protected function get(){
        return $this->request()->get();
    }

}